<?php

/**
 * partner actions.
 *
 * @package    sf_sandbox
 * @subpackage partner
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class partnerActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeEdit(sfWebRequest $request)
  {
 
    if ($request->isMethod('post'))
    {
      $u = sfGuardUserPeer::retrieveByPK($request->getParameter('user[id]'));
      $this->form = new partnerForm($u);
      $this->form->bind($request->getParameter('user'), $request->getFiles('user'));
      if ($this->form->isValid())
      {
        $this->form->save();
        $this->getUser()->setFlash('notice', __('Данные сохранены'));
        $this->redirect('partner/edit?id='.$this->form->getObject()->getId());
      }
    }
    else
    {
       if($this->getUser()->isSuperAdmin() or $request->getParameter('id')==$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ))
           {
               if($request->getParameter('id'))
               {
                   $u = sfGuardUserPeer::retrieveByPK($request->getParameter('id'));
               }
               else
               {
                   $u = new sfGuardUser();
               }
       $this->form = new partnerForm($u);
           }
           else
               {
                   $this->redirect('partner/edit?id='.$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
               }
    }

  }

  public function executeList(sfWebRequest $request)
  {
     $this->partners = sfGuardUserPeer::getAllPartners();
  }

    public function executeDel(sfWebRequest $request)
  {
     $p = sfGuardUserPeer::retrieveByPK($request->getParameter('id'));
     if($p) $p->delete();
     $this->redirect('partner/list');
  }

      public function executeSend_data(sfWebRequest $request)
  {
     $p = sfGuardUserPeer::retrieveByPK($request->getParameter('id'));
     if($p)
     {
         $this->getUser()->setFlash('notice',  __('Данные отправлены') );
     }
     $this->redirect('partner/list');
  }


   public function executeLogin(sfWebRequest $request)
  {
      $user = sfGuardUserPeer::retrieveByPK($request->getParameter('id'));
       $this->getContext()->getUser()->signIn($user);
      $this->redirect('sfAdminDash/dashboard');
  }

  public function executeCalculation(sfWebRequest $request)
  {

      $c = new Criteria();
      $c->add(sfGuardUserPeer::IS_PARTNER,true);
      $this->partner = sfGuardUserPeer::doSelect($c);

  }

}
